package com.ai.manager.edu.cms.device.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.stereotype.Component;

import com.ai.common.utils.cache.redis.shared.RedisUtils;
import com.ai.common.utils.lang.json.JsonUtils;
import com.ai.common.utils.lang.string.StringUtils;
import com.ai.common.utils.pagination.PaginationUtil;
import com.ai.dao.edu.cms.device.UserOrgInfoMapper;
import com.ai.manager.edu.cms.device.UserOrgInfoManager;
import com.ai.model.edu.user.UserOrgInfo;
import com.ai.model.edu.user.dto.UserOrgInfoDTO;
import com.ai.model.edu.user.vo.UserOrgInfoVO;
@Component("userOrgInfoManager")
public class UserOrgInfoManagerImpl implements UserOrgInfoManager{
	
	private Log log = LogFactory.getLog(getClass());

	@Resource private UserOrgInfoMapper userOrgInfoMapper;
	
	@Resource
	private SqlSessionTemplate sqlSessionTemplate;
	@Resource
	private RedisUtils redisUtils;
	
	@Override
	public List<UserOrgInfoVO> findUserOrgInfoListByUserIds(Map<String,Object> map) {
		
		return userOrgInfoMapper.findUserOrgInfoListByUserIds(map);
	}

	@Override
	public void batchInsertUserOrgInfo(List<UserOrgInfo> list) {
		userOrgInfoMapper.batchInsertUserOrgInfo(list);
		
	}

	@Override
	public void batchUpdateUserOrgInfo(List<UserOrgInfo> list) {
		List<List<UserOrgInfo>> split = PaginationUtil.split(list, 500);
		//批量
		SqlSession session = sqlSessionTemplate.getSqlSessionFactory().openSession(ExecutorType.BATCH);
		try {
			UserOrgInfoMapper mapper = session.getMapper(UserOrgInfoMapper.class);
			for(List<UserOrgInfo> tmp : split){
				mapper.batchUpdateUserOrgInfo(tmp);
				session.commit();
				session.clearCache();
			}
		} catch (Exception e) {
			log.error("批量更新异常" , e);
			session.rollback();
		} finally{
			session.close();
		}
		
		
	}

	@Override
	public List<UserOrgInfoVO> fetchMessagesTouchUsersByOrgs(UserOrgInfoDTO dto) {
		return userOrgInfoMapper.fetchMessagesTouchUsersByOrgs(dto);
	}

	@Override
	public int countMessageTouchUsersByOrgs(UserOrgInfoDTO dto) {
		return userOrgInfoMapper.countMessageTouchUsersByOrgs(dto);
	}

    @Override
    public void batchUpdateUserOrgInfoByContactPhones(List<String> phones) {
        List<List<String>> tempPhoneList = PaginationUtil.split(phones, 500);
        SqlSession session = sqlSessionTemplate.getSqlSessionFactory().openSession(ExecutorType.BATCH);
        try {
            UserOrgInfoMapper mapper = session.getMapper(UserOrgInfoMapper.class);
            for(List<String> tmp : tempPhoneList){
                mapper.batchUpdateUserOrgInfoIsSendByContactPhones(tmp);
                session.commit();
                session.clearCache();
            }
        } catch (Exception e) {
            log.error("批量更新异常" , e);
            session.rollback();
        }finally{
            session.close();
        }
    }

	@Override
	public List<UserOrgInfoVO> findUserOrgInfoListByUserIdsTmp(
			List<Long> tmpUserList) {
		List<UserOrgInfoVO> resultList = new ArrayList<UserOrgInfoVO>();
//		List<Long> userIds = new ArrayList<Long>();
//		for(Long userId : tmpUserList){
//			String json = redisUtils.get(UserOrgInfoVO.USER_ORG_INFO_KEY + userId);
//			if(StringUtils.isNotBlank(json)){
//				UserOrgInfoVO infoVO = JsonUtils.fromJson(json, UserOrgInfoVO.class);
//				resultList.add(infoVO);
//			}else{
//				userIds.add(userId);
//			}
//		}
		if(tmpUserList.size() > 0){
			List<UserOrgInfoVO> list = userOrgInfoMapper.findUserOrgInfoListByUserIdsTmp(tmpUserList);
			resultList.addAll(list);
//			try {
//				for(UserOrgInfoVO org : list){
//					redisUtils.setex(UserOrgInfoVO.USER_ORG_INFO_KEY +org.getUserId() , 86400*7, JsonUtils.toJson(org));
//				}
//			} catch (Exception e) {
//				log.error("批量放缓存异常" , e);
//			}
		}
		return resultList;
	}

}
